<?php
//
defined('BY_JYA') or exit('error');
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$order_status=$this->type__order_status();
$paytypes=array('0'=>'未付款','1'=>'已付款');
$return['setting']=$this->plugin_setting('basic');
if($_RQ['op']=='list'){
	$status = intval($_RQ['status']);
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'is_del'=>0);
	if($_RQ['ordersn']){
		$params['ordersn like']='%'.trim($_RQ['ordersn']).'%';
		$return['params']['ordersn']=trim($_RQ['ordersn']);
	}
	if(isset($_RQ['is_pay']) && $_RQ['is_pay']!=100){
		$return['params']['is_pay']=$_RQ['is_pay'];
		$params['is_pay']=intval($_RQ['is_pay']);
	}else{
		$return['params']['is_pay']=100;
	}
    $fields=array('id','store_id','mid','ordersn','price','status','is_pay','pay_id','print_status','remark','createtime');
    $orderby='id desc';
	$total='';
	$list=pdo_getslice('jy_meye_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	if($list){
		foreach($list as &$item){
			
			$item['goods']=pdo_getall('jy_meye_order_goods',array('order_id'=>$item['id']));
			
			$item['storename']=pdo_getcolumn('jy_meye_store',array('id'=>$item['store_id']),'title');
			
			$item['paytype']=$paytypes[$item['is_pay']];
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			if($item['goods']){
				foreach($item['goods'] as &$g){
					$g['goods_package']=$g['goods_package']?json_decode($g['goods_package'],true):array();
					if(!empty($g['goods_package'])){
						$totalprice =0;
						$goods_title = $g['goods_title'].':';
						foreach($g['goods_package'] as &$p){
							$totalprice += $p['num']*$p['price'];
							$goods_title .= '['.$p['title'].'-￥'.$p['price'].'*'.$p['num'].']';
						}
					}else{
						$goods_title = $g['goods_title'].' × '.$g['goods_num'];
						$totalprice = $g['goods_num']*$g['goods_price'];
					}
					$g['totalprice']=$totalprice;
					$g['goods_title']=$goods_title;
					$item['goods_num'] +=$g['goods_num'];
					$g['image']=pdo_getcolumn('jy_meye_goods',array('id'=>$g['goods_id']),'image');
				}
			}
		}
	}
	$return['list']=$list;
	$return['status']=$status;
	set_html('title','订单列表');
	exi($return);
}
if($_RQ['op']=='detail'){
	if(intval($_RQ['id'])){
		$item=pdo_get('jy_meye_order',array('id'=>$_RQ['id']));
		$return['item']=$item;
		exi($return);
	}else{
		exi('订单id不存在','error','jy_meye/order/');	
	}
}
if($_RQ['op']=='cancel'){
	if($_RQ['id']){
		$item=pdo_get('jy_meye_order',array('id'=>$_RQ['id']));
		if($item['status']==0 && $item['is_pay']==1){
			$this->order_change(array('status'=>5,'order_id'=>$_RQ['id']));
			exi('取消成功','success');
		}else{
			exi('只有未确认且已支付订单可以取消','error');
		}
	}else{
		exi('无效订单','error');
	}
}
if($_RQ['op']=='receive'){
	if($_RQ['id']){
		$item=pdo_get('jy_meye_order',array('id'=>$_RQ['id']));
		$this->order_change(array('status'=>3,'order_id'=>$_RQ['id']));
		exi('确认成功','success');
	}else{
		exi('无效订单','error');
	}
}
if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_update('jy_meye_order',array('is_del'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除订单成功');
}
if($_RQ['op']=='pay'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	$pay_id=pdo_getcolumn('jy_meye_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'pay_id');
	exi($pay_id);
}
if($_RQ['op']=='refund'){
	if($_RQ['id']){
		$item=pdo_get('jy_meye_order',array('id'=>$_RQ['id']));
		if($item['is_pay']==1 && $item['status']!=5){
			$this->order_change(array('status'=>5,'order_id'=>intval($_RQ['id'])));
			exi('申请退款成功','success');
		}else{
			exi('只有已支付且未退款订单可以申请退款','error');
		}
	}
}	
?>